import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../config/config.dart';

/// 屏幕中间弹出提示
class Alert {

  /// 提示性的，只有确认可以点击(默认原生样式)
  static void showNativeTip({
    required BuildContext context,
    String? title,
    String? contentText,
    Function? action,
    String? closeText,
  }) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: title != null ? Text(title) : null,
          content: contentText != null ? Text(contentText) : null,
          actions: <Widget>[
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
                if (action != null) action();
              },
              child: Text(closeText ?? '确定'),
            ),
            // FlatButton(
            //   child: Text(closeText ?? '确定'),
            //   onPressed: () {
            //   },
            // ),
          ],
        );
      },
    );
  }

  /// 不能消失
  static void showNativeWithoutDismiss(String tipText, {required BuildContext context}) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext content) {
        return AlertDialog(
          title: Text(tipText),
          actions: const [],
        );
      },
    );
  }

  /// 自定义弹窗(默认原生样式)
  static void showNativeAlert({
    required BuildContext context,
    required String title,
    String? content,
    String leftText = "取消",
    String rightText = "确定",
    Function? leftAction,
    Function? rightAction,
    bool? onlyRight,
  }) {
    if (FWConfig.isIOS) {
      showCupertinoDialog(
          context: context,
          builder: (context2) {
            return CupertinoAlertDialog(
              title: Text(
                title,
                style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 17),
              ),
              content: content != null ? Text(content) : null,
              actions: <Widget>[
                if (onlyRight != true)
                  CupertinoDialogAction(
                    child: Text(leftText),
                    onPressed: () {
                      Navigator.of(context2).pop();
                      if (leftAction != null) leftAction();
                    },
                  ),
                CupertinoDialogAction(
                  child: Text(rightText),
                  onPressed: () {
                    Navigator.of(context2).pop();
                    if (rightAction != null) rightAction();
                  },
                ),
              ],
            );
          });
    } else {
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context2) {
          return AlertDialog(
            title: Text(
              title,
              style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 17),
            ),
            content: content != null ? Text(content) : null,
            actions: <Widget>[
              TextButton(
                onPressed: () {
                  Navigator.of(context2).pop();
                  if (leftAction != null) leftAction();
                },
                child: Text(leftText),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context2).pop();
                  if (rightAction != null) rightAction();
                },
                child: Text(rightText),
              ),
              // FlatButton(
              //   child: Text(leftText),
              //   onPressed: () {
              //     Navigator.of(context2).pop();
              //     if (leftAction != null) leftAction();
              //   },
              // ),
              // FlatButton(
              //   child: Text(rightText),
              //   onPressed: () {
              //     Navigator.of(context2).pop();
              //     if (rightAction != null) rightAction();
              //   },
              // )
            ],
          );
        },
      );
    }
  }

}
